// index.js
const app = getApp();

Page({
  data: {
    banners: [
      {
        id: 1,
        imageUrl: 'https://picsum.photos/id/1018/750/300',
        type: 'product',
        url: '/pages/detail/detail?id=101'
      },
      {
        id: 2,
        imageUrl: 'https://picsum.photos/id/1019/750/300',
        type: 'theme',
        url: '/pages/search/search?themeId=nature'
      },
      {
        id: 3,
        imageUrl: 'https://picsum.photos/id/1035/750/300',
        type: 'region',
        url: '/pages/search/search?regionId=qinghai'
      }
    ],
    navMenu: [
      {
        id: 1,
        name: '国内游',
        icon: 'location',
        bgColor: '#FF4D6A',
        type: 'category',
        url: '/pages/search/search?categoryId=domestic'
      },
      {
        id: 2,
        name: '出境游',
        icon: 'location',
        bgColor: '#1890FF',
        type: 'category',
        url: '/pages/search/search?categoryId=outbound'
      },
      {
        id: 3,
        name: '周边游',
        icon: 'location',
        bgColor: '#52C41A',
        type: 'category',
        url: '/pages/search/search?categoryId=surrounding'
      },
      {
        id: 4,
        name: '定制游',
        icon: 'edit',
        bgColor: '#FAAD14',
        type: 'category',
        url: '/pages/search/search?categoryId=customized'
      },
      {
        id: 5,
        name: '主题游',
        icon: 'star',
        bgColor: '#722ED1',
        type: 'category',
        url: '/pages/search/search?categoryId=theme'
      },
      {
        id: 6,
        name: '亲子游',
        icon: 'heart',
        bgColor: '#13C2C2',
        type: 'category',
        url: '/pages/search/search?categoryId=family'
      },
      {
        id: 7,
        name: '户外探险',
        icon: 'location',
        bgColor: '#EB2F96',
        type: 'category',
        url: '/pages/search/search?categoryId=outdoor'
      },
      {
        id: 8,
        name: '全部分类',
        icon: 'search',
        bgColor: '#666666',
        type: 'more',
        url: '/pages/search/search'
      }
    ],
    themes: [
      {
        id: 1,
        name: '自然风光',
        imageUrl: 'https://picsum.photos/id/1036/200/200',
        type: 'theme',
        url: '/pages/search/search?themeId=nature'
      },
      {
        id: 2,
        name: '人文历史',
        imageUrl: 'https://picsum.photos/id/1037/200/200',
        type: 'theme',
        url: '/pages/search/search?themeId=culture'
      },
      {
        id: 3,
        name: '美食之旅',
        imageUrl: 'https://picsum.photos/id/1080/200/200',
        type: 'theme',
        url: '/pages/search/search?themeId=food'
      },
      {
        id: 4,
        name: '摄影之旅',
        imageUrl: 'https://picsum.photos/id/1039/200/200',
        type: 'theme',
        url: '/pages/search/search?themeId=photography'
      },
      {
        id: 5,
        name: '奢华体验',
        imageUrl: 'https://picsum.photos/id/1040/200/200',
        type: 'theme',
        url: '/pages/search/search?themeId=luxury'
      }
    ],
    hotProducts: [
      {
        id: 101,
        title: '美宿 | 西北寻秋 青甘环线7天6晚',
        regionName: '青甘',
        price: 8380,
        originalPrice: 9999,
        rating: 4.8,
        salesCount: 256,
        imageUrl: 'https://picsum.photos/id/1016/200/200'
      },
      {
        id: 102,
        title: '美宿 | 极致装达木 青海5天4晚',
        regionName: '青海',
        price: 6580,
        originalPrice: 7999,
        rating: 4.7,
        salesCount: 189,
        imageUrl: 'https://picsum.photos/id/1019/200/200'
      },
      {
        id: 103,
        title: '轻奢 | 风马遗野 呼伦贝尔4晚5天',
        regionName: '内蒙',
        price: 7980,
        originalPrice: 8999,
        rating: 4.8,
        salesCount: 210,
        imageUrl: 'https://picsum.photos/id/1018/200/200'
      }
    ],
    destinations: [
      {
        id: 1,
        name: '青海湖',
        description: '中国最大的内陆咸水湖',
        imageUrl: 'https://picsum.photos/id/1036/400/200'
      },
      {
        id: 2,
        name: '敦煌',
        description: '丝绸之路上的明珠',
        imageUrl: 'https://picsum.photos/id/1037/400/200'
      },
      {
        id: 3,
        name: '张掖丹霞',
        description: '色彩斑斓的地貌奇观',
        imageUrl: 'https://picsum.photos/id/1038/400/200'
      },
      {
        id: 4,
        name: '西藏',
        description: '雪域高原的神秘之地',
        imageUrl: 'https://picsum.photos/id/1039/400/200'
      }
    ],
    familyProducts: [
      {
        id: 201,
        title: '亲子 | 青海湖环湖骑行3天2晚',
        regionName: '青海',
        price: 3980,
        originalPrice: 4599,
        rating: 4.9,
        salesCount: 320,
        imageUrl: 'https://picsum.photos/id/1040/200/200'
      },
      {
        id: 202,
        title: '亲子 | 呼伦贝尔草原牧场体验2天1晚',
        regionName: '内蒙',
        price: 2580,
        originalPrice: 2999,
        rating: 4.8,
        salesCount: 278,
        imageUrl: 'https://picsum.photos/id/1041/200/200'
      },
      {
        id: 203,
        title: '亲子 | 张掖丹霞地质公园科普之旅3天2晚',
        regionName: '甘肃',
        price: 3280,
        originalPrice: 3799,
        rating: 4.7,
        salesCount: 195,
        imageUrl: 'https://picsum.photos/id/1042/200/200'
      }
    ]
  },

  onLoad: function() {
    // 检查登录状态
    this.checkLoginStatus();
    
    // 获取数据
    this.fetchData();
  },

  onShow: function() {
    // 如果需要每次显示页面都刷新数据，可以在这里调用
  },

  onPullDownRefresh: function() {
    // 下拉刷新
    this.fetchData();
    wx.stopPullDownRefresh();
  },

  // 检查登录状态
  checkLoginStatus: function() {
    // 检查是否已登录
    const isLoggedIn = app.globalData.isLoggedIn;
    
    if (!isLoggedIn) {
      // 获取用户信息
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            wx.getUserInfo({
              success: res => {
                app.globalData.userInfo = res.userInfo;
                app.globalData.isLoggedIn = true;
              }
            });
          }
        }
      });
    }
  },

  // 获取数据
  fetchData: function() {
    // 实际项目中应该从服务器获取数据
    // 这里使用模拟数据
    
    // 获取轮播图
    // wx.cloud.callFunction({
    //   name: 'getBanners',
    //   success: res => {
    //     if (res.result && res.result.data) {
    //       this.setData({
    //         banners: res.result.data
    //       });
    //     }
    //   },
    //   fail: err => {
    //     console.error('获取轮播图失败', err);
    //   }
    // });
    
    // 获取热门路线
    // wx.cloud.callFunction({
    //   name: 'getHotProducts',
    //   success: res => {
    //     if (res.result && res.result.data) {
    //       this.setData({
    //         hotProducts: res.result.data
    //       });
    //     }
    //   },
    //   fail: err => {
    //     console.error('获取热门路线失败', err);
    //   }
    // });
    
    // 获取目的地推荐
    // wx.cloud.callFunction({
    //   name: 'getDestinations',
    //   success: res => {
    //     if (res.result && res.result.data) {
    //       this.setData({
    //         destinations: res.result.data
    //       });
    //     }
    //   },
    //   fail: err => {
    //     console.error('获取目的地推荐失败', err);
    //   }
    // });
    
    // 获取亲子精选
    // wx.cloud.callFunction({
    //   name: 'getFamilyProducts',
    //   success: res => {
    //     if (res.result && res.result.data) {
    //       this.setData({
    //         familyProducts: res.result.data
    //       });
    //     }
    //   },
    //   fail: err => {
    //     console.error('获取亲子精选失败', err);
    //   }
    // });
  },

  // 跳转到搜索页
  goToSearch: function() {
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },

  // 点击轮播图
  onBannerTap: function(e) {
    const { id, type, url } = e.currentTarget.dataset;
    
    if (url) {
      wx.navigateTo({
        url: url
      });
    }
  },

  // 点击导航菜单
  onNavTap: function(e) {
    const { id, type, url } = e.currentTarget.dataset;
    
    if (url) {
      wx.navigateTo({
        url: url
      });
    }
  },

  // 跳转到更多页面
  goToMore: function(e) {
    const { type } = e.currentTarget.dataset;
    
    let url = '/pages/search/search';
    
    switch (type) {
      case 'theme':
        url = '/pages/search/search?type=theme';
        break;
      case 'hot':
        url = '/pages/search/search?type=hot';
        break;
      case 'destination':
        url = '/pages/search/search';
        break;
      case 'family':
        url = '/pages/search/search?type=family';
        break;
    }
    
    wx.navigateTo({
      url: url
    });
  },

  // 跳转到主题页
  goToTheme: function(e) {
    const { id } = e.currentTarget.dataset;
    
    wx.navigateTo({
      url: `/pages/search/search?themeId=${id}`
    });
  },

  // 跳转到详情页
  goToDetail: function(e) {
    const { id } = e.currentTarget.dataset;
    
    wx.navigateTo({
      url: `/pages/detail/detail?id=${id}`
    });
  },

  // 跳转到目的地页
  goToDestination: function(e) {
    const { id } = e.currentTarget.dataset;
    
    wx.navigateTo({
      url: `/pages/search/search?regionId=${id}`
    });
  }
});